home *** CD-ROM | disk | FTP | other *** search
/ Zoom 2 / Zoom - Release 2 (1996)(Active Software)[!].iso / programming / amiga / muibuilder / mb / developer / c / sources_gencodec / writemainfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-17  |  13.7 KB  |  451 lines

  1. #include "WriteMainFile.h"
  2. #include "Tools.h"
  3. #include "MB_protos.h"
  4. #include "MB_pragmas.h"
  5. #include "mb.h"
  6.  
  7. #include <clib/exec_protos.h>
  8. #include <clib/dos_protos.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. extern void Quit(void); 
  13.  
  14. #define HEADER_END_MSG                 "/* GenCodeC header end */"
  15. #define    HEADER_END                    HEADER_END_MSG "\n"
  16. #define    LIBRARY_DECLARATION_MSG        "/* Declarations for libraries (inserted by GenCodeC) */"
  17. #define    LIBRARY_DECLARATION            LIBRARY_DECLARATION_MSG "\n"
  18. #define    LIBRARY_MUI_MSG                "struct Library * MUIMasterBase"
  19. #define    LIBRARY_MUI                    LIBRARY_MUI_MSG ";\n"
  20. #define    LIBRARY_LOCALE_MSG            "struct Library * LocaleBase"
  21. #define    LIBRARY_LOCALE                LIBRARY_LOCALE_MSG ";\n"
  22. #define OPEN_LOCALE_LIBRARY_MSG        "if (!(LocaleBase = OpenLibrary(\"locale.library\",38)))"
  23. #define OPEN_LOCALE_LIBRARY            "\t" OPEN_LOCALE_LIBRARY_MSG "\n"
  24. #define CLOSE_LOCALE_LIBRARY_MSG    "CloseLibrary(LocaleBase)"
  25. #define CLOSE_LOCALE_LIBRARY        CLOSE_LOCALE_LIBRARY_MSG ";\n"
  26. #define    INIT_BEGIN_MSG                "/* Init() function */"
  27. #define    INIT_BEGIN                    "\n" INIT_BEGIN_MSG "\n"
  28. #define    INIT_END_MSG                "/* GenCodeC init() end */"
  29. #define    INIT_END                    "}\n" INIT_END_MSG "\n"
  30. #define    END_BEGIN_MSG                "/* End() function */"
  31. #define    END_BEGIN                    "\n" END_BEGIN_MSG "\n"
  32. #define END_FUNCTION_MSG            "void end( void )"
  33. #define END_FUNCTION                END_FUNCTION_MSG "\n{\n"
  34. #define    END_END_MSG                    "/* GenCodeC end() end */"
  35. #define    END_END                        "}\n" END_END_MSG "\n"
  36. #define    OPEN_CATALOG_MSG            "OpenAppCatalog(NULL,NULL)"
  37. #define    OPEN_CATALOG                OPEN_CATALOG_MSG ";\n"
  38. #define    CLOSE_CATALOG_MSG            "CloseAppCatalog()"
  39. #define    CLOSE_CATALOG                "\t" CLOSE_CATALOG_MSG ";\n"
  40. #define    CASE_BEGIN_MSG                "/* Insert your code between the \"case\" statement and comment \"end of case ...\" */"
  41. #define    CASE_BEGIN                    CASE_BEGIN_MSG "\n"
  42. #define    CASE_END_MSG                "/* End computing of IDCMP */"
  43. #define    CASE_END                    CASE_END_MSG "\n"
  44.  
  45. /****************************************************************************************************************/
  46. /*****                                                                                                        *****/
  47. /**                                                 WriteIDCMP                                                       **/
  48. /*****                                                                                                        *****/
  49. /****************************************************************************************************************/
  50.  
  51. static void WriteIDCMP(FILE *file,char *adr_file,char *buf,ULONG varnb)
  52. {
  53.     int        i;
  54.     char     *name;
  55.     char    *tmp;
  56.     ULONG    type;
  57.     char     *str;
  58.     char    *str1;
  59.     char    ctmp;
  60.  
  61.     /* Write IDCMPs */
  62.     for(i=0;i<varnb;i++)
  63.     {
  64.         MB_GetVarInfo(i,
  65.                       MUIB_VarType,&type,
  66.                       MUIB_VarName,&name,
  67.                       TAG_END);
  68.         if (!(tmp = AllocMemory(strlen(name)+19)))
  69.         {
  70.             fclose(file);
  71.             FreeMemory(adr_file);
  72.             Quit();
  73.         }
  74.         strcpy(tmp,"case ");
  75.         strcat(tmp,name);
  76.         strcat(tmp,":");
  77.         if (type == TYPEVAR_IDENT)
  78.         {
  79.             if (str = strstr(buf,tmp))
  80.             {
  81.                 /* Write old IDCMP */
  82.                 strcpy(tmp,"/* end of case ");
  83.                 strcat(tmp,name);
  84.                 strcat(tmp," */");
  85.                 if (!(str1=strstr(str,tmp)))
  86.                 {
  87.                     char *msg;
  88.  
  89.                     if (!(msg = (char *)AllocMemory(60+strlen(tmp))))
  90.                     {
  91.                         fclose(file);
  92.                         FreeMemory(buf);
  93.                         Quit();
  94.                     }
  95.                     sprintf(msg,"Can't find  \"%s\"\n in MainFile generated by GenCodeC\n\n",tmp);
  96.                     DisplayMsg(msg);
  97.                     FreeMemory(msg);
  98.                     fclose(file);
  99.                     FreeMemory(buf);
  100.                     Quit();
  101.                 }
  102.                 str1+=strlen(tmp);
  103.                 ctmp=*str1;
  104.                 *str1='\0';
  105.                 fprintf(file,"\t\t\t%s\n\n",str);
  106.                 *str1=ctmp;
  107.             }
  108.             else 
  109.             {
  110.                 /* Write NEW IDCMP */
  111.                 fprintf(file,"\t\t\tcase %s:\n",name);
  112.                 fprintf(file,"\t\t\t/* end of case %s */\n\n",name);
  113.             }
  114.         }
  115.         FreeMemory(tmp);
  116.     }
  117. }
  118.  
  119. /****************************************************************************************************************/
  120. /*****                                                                                                        *****/
  121. /**                                                 WriteIncludes                                                   **/
  122. /*****                                                                                                        *****/
  123. /****************************************************************************************************************/
  124.  
  125. static char *WriteIncludes(FILE *file,char *adr_file,char *HeaderFile,char *MainFile,BOOL OldMainExists) 
  126. {
  127.     if (!OldMainExists)
  128.     {
  129.         fprintf(file,"\n/* Include generated by GenCodeC */\n");
  130.         fprintf(file,"#include \"%s\"\n\n",FilePart(HeaderFile));
  131.  
  132.         return NULL;
  133.     }
  134.     else
  135.     {
  136.         return CopyBlock(file,adr_file,adr_file,HEADER_END,LIBRARY_DECLARATION,
  137.                         HEADER_END_MSG,LIBRARY_DECLARATION_MSG,MainFile);
  138.     }
  139. }
  140.  
  141. /****************************************************************************************************************/
  142. /*****                                                                                                        *****/
  143. /**                                                 WriteDeclarations                                               **/
  144. /*****                                                                                                        *****/
  145. /****************************************************************************************************************/
  146.  
  147. static char *WriteDeclarations(FILE *file,char *adr_file,char *str,char *MainFile,BOOL OldMainExists,BOOL Locale) 
  148. {
  149.     if (!OldMainExists)
  150.     {
  151.         fprintf(file,LIBRARY_DECLARATION);
  152.         fprintf(file,LIBRARY_MUI);
  153.         if (Locale)
  154.                 fprintf(file,LIBRARY_LOCALE);
  155.  
  156.         return NULL;
  157.     }
  158.     else
  159.     {
  160.         fprintf(file,"%s",LIBRARY_DECLARATION);
  161.         str = CopyBlock(file,adr_file,str,LIBRARY_DECLARATION,LIBRARY_MUI,
  162.                         LIBRARY_DECLARATION_MSG,LIBRARY_MUI_MSG,MainFile);
  163.         fprintf(file,"%s",LIBRARY_MUI);
  164.         if (Locale && (!strstr(str,LIBRARY_LOCALE)))
  165.         {
  166.             fprintf(file,"%s",LIBRARY_LOCALE);
  167.             str = CopyBlock(file,adr_file,str,LIBRARY_MUI,INIT_BEGIN,
  168.                             LIBRARY_MUI_MSG,INIT_BEGIN_MSG,MainFile);
  169.         }
  170.         else
  171.         {
  172.             if (!Locale && (strstr(str,LIBRARY_LOCALE)))
  173.                 str = CopyBlock(file,adr_file,str,LIBRARY_LOCALE,INIT_BEGIN,
  174.                                 LIBRARY_LOCALE_MSG,INIT_BEGIN_MSG,MainFile);
  175.             else
  176.                 str = CopyBlock(file,adr_file,str,LIBRARY_MUI,INIT_BEGIN,
  177.                                 LIBRARY_MUI_MSG,INIT_BEGIN_MSG,MainFile);
  178.         }
  179.         return str;
  180.     }
  181. }
  182.  
  183. /****************************************************************************************************************/
  184. /*****                                                                                                        *****/
  185. /**                                             WriteInitFunction                                                   **/
  186. /*****                                                                                                        *****/
  187. /****************************************************************************************************************/
  188.  
  189. static char *WriteInitFunction(FILE *file,char *adr_file,char *str,char *MainFile,BOOL OldMainExists,BOOL Locale)
  190. {
  191.     if (!OldMainExists)
  192.     {
  193.         fprintf(file,INIT_BEGIN);
  194.         fprintf(file,"void init( void )\n");
  195.         fprintf(file,"{\n");
  196.         fprintf(file,"\tif (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME,MUIMASTER_VMIN)))\n");
  197.         fprintf(file,"\t{\n");
  198.         fprintf(file,"\t\tprintf(\"Can't Open MUIMaster Library\\n\");\n");
  199.         fprintf(file,"\t\texit(20);\n");
  200.         fprintf(file,"\t}\n");
  201.         if (Locale)
  202.         {
  203.             fprintf(file,"%s",OPEN_LOCALE_LIBRARY);
  204.             fprintf(file,"\t{\n");
  205.             fprintf(file,"\t\tprintf(\"Can't Open Locale Library\\n\");\n");
  206.             fprintf(file,"\t\tprintf(\"Built-in Language will be used !!!\\n\");\n");
  207.             fprintf(file,"\t}\n");
  208.             fprintf(file,"\tOpenAppCatalog(NULL,NULL);\n");
  209.         }
  210.         fprintf(file,INIT_END);
  211.     
  212.         return NULL;
  213.     }
  214.     else
  215.     {
  216.         fprintf(file,INIT_BEGIN);
  217.         if (Locale && !(strstr(str,OPEN_LOCALE_LIBRARY)))
  218.         {
  219.             str = CopyBlock(file,adr_file,str,INIT_BEGIN,INIT_END,
  220.                             INIT_BEGIN_MSG,INIT_END_MSG,MainFile);
  221.             fprintf(file,"%s",OPEN_LOCALE_LIBRARY);
  222.             fprintf(file,"\t{\n");
  223.             fprintf(file,"\t\tprintf(\"Can't Open Locale Library\\n\");\n");
  224.             fprintf(file,"\t\tprintf(\"Built-in Language will be used !!!\\n\");\n");
  225.             fprintf(file,"\t}\n");
  226.             fprintf(file,"\tOpenAppCatalog(NULL,NULL);\n");
  227.         }
  228.         else
  229.         {
  230.             if (!Locale && (strstr(str,OPEN_LOCALE_LIBRARY)))
  231.             {
  232.                 str = CopyBlock(file,adr_file,str,INIT_BEGIN,OPEN_LOCALE_LIBRARY,
  233.                                 INIT_BEGIN_MSG,OPEN_LOCALE_LIBRARY_MSG,MainFile);
  234.                 str = CopyBlock(file,adr_file,str,OPEN_CATALOG,INIT_END,
  235.                                 OPEN_CATALOG_MSG,INIT_END_MSG,MainFile);
  236.             }
  237.             else 
  238.             {
  239.                 str = CopyBlock(file,adr_file,str,INIT_BEGIN,INIT_END,
  240.                                 INIT_BEGIN_MSG,INIT_END_MSG,MainFile);
  241.             }
  242.         }
  243.         fprintf(file,INIT_END);
  244.  
  245.         return str;
  246.     }
  247. }
  248.  
  249. /****************************************************************************************************************/
  250. /*****                                                                                                        *****/
  251. /**                                             WriteEndFunction                                                   **/
  252. /*****                                                                                                        *****/
  253. /****************************************************************************************************************/
  254.  
  255. static char *WriteEndFunction(FILE *file,char *adr_file,char *str,char *MainFile,BOOL OldMainExists,BOOL Locale)
  256. {
  257.     if (!OldMainExists)
  258.     {
  259.         fprintf(file,END_BEGIN);
  260.         fprintf(file,END_FUNCTION);
  261.         if (Locale)
  262.         {
  263.             fprintf(file,"%s",CLOSE_CATALOG);
  264.             fprintf(file,"\tif (LocaleBase)\n");
  265.             fprintf(file,"\t\t%s",CLOSE_LOCALE_LIBRARY);
  266.         }
  267.         fprintf(file,"\tCloseLibrary(MUIMasterBase);\n");
  268.         fprintf(file,"\texit(20);\n");
  269.         fprintf(file,END_END);
  270.         fprintf(file,"\n");
  271.  
  272.         return NULL;
  273.     }
  274.     else
  275.     {
  276.         fprintf(file,END_BEGIN);
  277.         if (Locale && !(strstr(str,CLOSE_CATALOG)))
  278.         {
  279.             fprintf(file,END_FUNCTION);
  280.             fprintf(file,"%s",CLOSE_CATALOG);
  281.             fprintf(file,"\tif (LocaleBase)\n");
  282.             fprintf(file,"\t\t%s",CLOSE_LOCALE_LIBRARY);
  283.             str = CopyBlock(file,adr_file,str,END_FUNCTION,END_END,
  284.                             END_FUNCTION_MSG,END_END_MSG,MainFile);
  285.         }
  286.         else
  287.         {
  288.             if (!Locale && (strstr(str,CLOSE_CATALOG)))
  289.             {
  290.                 str = CopyBlock(file,adr_file,str,END_BEGIN,CLOSE_CATALOG,
  291.                                 END_BEGIN_MSG,CLOSE_CATALOG_MSG,MainFile);
  292.                 str = CopyBlock(file,adr_file,str,CLOSE_LOCALE_LIBRARY,END_END,
  293.                                 CLOSE_LOCALE_LIBRARY_MSG,END_END_MSG,MainFile);
  294.             }
  295.             else 
  296.             {
  297.                 str = CopyBlock(file,adr_file,str,END_BEGIN,END_END,
  298.                                 END_BEGIN_MSG,END_END_MSG,MainFile);
  299.             }
  300.         }
  301.         fprintf(file,END_END);
  302.  
  303.         return str;
  304.     }
  305. }
  306.  
  307. /****************************************************************************************************************/
  308. /*****                                                                                                        *****/
  309. /**                                             WriteMainFunction                                                   **/
  310. /*****                                                                                                        *****/
  311. /****************************************************************************************************************/
  312.  
  313. static void WriteMainFunction(FILE *file,char *adr_file,char *str,char *MainFile,char *name,
  314.                               BOOL OldMainExists,ULONG varnb)
  315. {
  316.     if (!OldMainExists)
  317.     {
  318.         fprintf(file,"/* Main Function inserted by GenCodeC */\n");
  319.         fprintf(file,"int main(int argc,char **argv)\n");
  320.         fprintf(file,"{\n");
  321.         fprintf(file,"\tstruct Obj%s * %s = NULL;    /* Object */\n",name,name);
  322.         fprintf(file,"\tBOOL\trunning = TRUE;\n");
  323.         fprintf(file,"\tULONG\tsignal;\n\n");
  324.         fprintf(file,"\t/* Program initialisation : generated by GenCodeC */\n");
  325.         fprintf(file,"\tinit();\n\n");
  326.         fprintf(file,"\t/* Create Object : generated by GenCodeC */\n");
  327.         fprintf(file,"\tif (!(%s = Create%s()))\n",name,name);
  328.         fprintf(file,"\t{\n");
  329.         fprintf(file,"\t\tprintf(\"Can't Create %s\\n\");\n",name);
  330.         fprintf(file,"\t\tend();\n");
  331.         fprintf(file,"\t}\n\n");
  332.         fprintf(file,"\twhile (running)\n");
  333.         fprintf(file,"\t{\n");
  334.         fprintf(file,"\t\tswitch (DoMethod(%s->%s,MUIM_Application_Input,&signal))\n",name,name);
  335.         fprintf(file,"\t\t{\n");
  336.         fprintf(file,"\t\t\tcase MUIV_Application_ReturnID_Quit:\n");
  337.         fprintf(file,"\t\t\t\trunning = FALSE;\n");
  338.         fprintf(file,"\t\t\t\tbreak;\n\n");
  339.         fprintf(file,"\t\t\t%s",CASE_BEGIN);
  340.     }
  341.     else
  342.     {
  343.         str = CopyBlock(file,adr_file,str,END_END,CASE_BEGIN,
  344.                         END_END_MSG,CASE_BEGIN_MSG,MainFile);
  345.         fprintf(file,CASE_BEGIN);
  346.     }
  347.  
  348.     if (!OldMainExists)
  349.         WriteIDCMP(file,adr_file,adr_file,varnb);
  350.     else
  351.         WriteIDCMP(file,adr_file,str,varnb);
  352.  
  353.     if (!OldMainExists)
  354.     {        
  355.         fprintf(file,"\t\t\t/* End computing of IDCMP */\n"); 
  356.         fprintf(file,"\t\t}\n");
  357.         fprintf(file,"\t\tif (running && signal) Wait(signal);\n");
  358.         fprintf(file,"\t}\n");
  359.         fprintf(file,"\tDispose%s(%s);\n",name,name);
  360.         fprintf(file,"\tend();\n");
  361.         fprintf(file,"}\n");
  362.     }
  363.     else
  364.     {
  365.         fprintf(file,"\t\t\t%s",CASE_END);
  366.         CopyBlock(file,adr_file,str,CASE_END,NULL,
  367.                   CASE_END_MSG,NULL,MainFile);
  368.     }
  369. }
  370.  
  371. /****************************************************************************************************************/
  372. /*****                                                                                                        *****/
  373. /**                                             WriteMainFile                                                       **/
  374. /*****                                                                                                        *****/
  375. /****************************************************************************************************************/
  376.  
  377. void WriteMainFile(char *HeaderFile,char *MainFile,char *MainHeaderText,ULONG varnb,BOOL Locale)
  378. {
  379.     ULONG                            length;
  380.     BPTR                            TMPfile;
  381.     BPTR                            BakFile;
  382.     char                            *adr_file = NULL;
  383.     char                            *str;
  384.     __aligned struct FileInfoBlock  Info;
  385.     char                            *buffer     = NULL;
  386.     char                            *name = NULL;
  387.     BOOL                            OldMainExists = FALSE;
  388.     FILE                            *file;
  389.  
  390.  
  391.     /* If the file already exists, we load it in memory and we save it in a .bak file*/
  392.     if (TMPfile = Open(MainFile, MODE_OLDFILE))
  393.     {
  394.         OldMainExists = TRUE;
  395.         ExamineFH(TMPfile, &Info);
  396.         length = Info.fib_Size;
  397.         if (!(adr_file = AllocMemory(length+1)))
  398.         {
  399.             Close(TMPfile);
  400.             Quit();
  401.         }
  402.         Read( TMPfile, adr_file, length);
  403.         adr_file[length] = '\0';
  404.         Close(TMPfile);
  405.         if (!(buffer = AllocMemory(strlen(MainFile)+4+1)))
  406.         {
  407.             Close(TMPfile);
  408.             Quit();
  409.         }
  410.         strcpy(buffer,MainFile);
  411.         strcat(buffer,".bak");
  412.         if (BakFile = Open(buffer,MODE_NEWFILE))
  413.         {
  414.             Write(BakFile,adr_file,length);
  415.             Close(BakFile);
  416.         }
  417.         FreeMemory(buffer);
  418.     }
  419.  
  420.     MB_GetVarInfo(0, MUIB_VarName, &name, TAG_END);
  421.  
  422.     if (file = fopen(MainFile,"w"))
  423.     {
  424.         fprintf(file,"/* Main-Header File inserted by GenCodeC */\n");
  425.  
  426.         /* Copy Main-Header into MainFile */
  427.         fprintf(file,"%s",MainHeaderText);
  428.  
  429.         fprintf(file,HEADER_END);
  430.         
  431.         str = WriteIncludes(file,adr_file,HeaderFile,MainFile,OldMainExists);
  432.  
  433.         str = WriteDeclarations(file,adr_file,str,MainFile,OldMainExists,Locale);
  434.  
  435.         str = WriteInitFunction(file,adr_file,str,MainFile,OldMainExists,Locale);
  436.  
  437.         if (OldMainExists)
  438.             str = CopyBlock(file,adr_file,str,INIT_END,END_BEGIN,
  439.                             INIT_END_MSG,END_BEGIN_MSG,MainFile);
  440.  
  441.         str = WriteEndFunction(file,adr_file,str,MainFile,OldMainExists,Locale);
  442.  
  443.         WriteMainFunction(file,adr_file,str,MainFile,name,OldMainExists,varnb);
  444.  
  445.         fclose(file);
  446.         FreeMemory(adr_file);
  447.     }
  448.     else
  449.         DisplayMsg("Unable to create Main-File !\n");
  450. }
  451.